xquery version "1.0";

declare namespace util="http://exist-db.org/xquery/util";
declare namespace dc="http://purl.org/dc/elements/1.1/";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace session="http://exist-db.org/xquery/session";

declare option exist:serialize "method=xhtml media-type=text/xml";

declare function local:rate($r,$rc) {
	if ($rc = 0) then 0
	else $r
};

declare function local:trackIds() {
        fn:subsequence(
	for $track in doc("Test1/Tracks.xml")//track
	order by number(local:rate(data($track/rating),data($track/rating-count))) descending
	return $track
	,1,10)
};

<rss version="2.0">
    <channel>
    <title>Top Rated Tracks</title>
    <link>http://localhost:9091/ad/index.html</link>
    <description>Top Rated tracks Exploration.</description>
    <language>en-us</language>
    <pubDate></pubDate>
    <lastBuildDate></lastBuildDate>
    <docs>http://localhost:9091/ad/index.html</docs>
    <generator>Weblog Editor 2.0</generator>
    <managingEditor>editor@example.com</managingEditor>
    <webMaster>webmaster@example.com</webMaster>
    <ttl>5</ttl>
    {
	let $tracks := doc("Test1/Tracks.xml")//tracks
	for $trackid in local:trackIds()
	return 
	             for $t1 in $tracks/track[@id=$trackid/@id]
	             return <item>
	             		<title>{data($t1/name)}</title>
	                    	<link>{data($t1/url)}</link>
	                    </item>
    }	
    </channel>
    
    
</rss>
			
